package cn.vivi.recyclercomp.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import cn.vivi.recyclercomp.fragment.RecyclerViewBaseFragment.LoadingLayout;

public abstract class RecyclerViewBaseAdapter<T> extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

	protected Context context;
	protected List<T> dataList;
	private AdapterView.OnItemClickListener mClickListener;
	private AdapterView.OnItemLongClickListener mLongClickListener;


	public RecyclerViewBaseAdapter(Context con, List<T> data) {
		this.context = con;
		if (data == null) {
			data = new ArrayList<T>();
		}
		dataList = data;
	}


	@Override
	public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
		if (viewType == ITEM_VIEW_TYPE_HEADER) {
			return new HeaderViewHolder(mHeaderView);

		} else if (viewType == ITEM_VIEW_TYPE_LOADING) {
			LoadingViewHolder h = new LoadingViewHolder(mLoadingLayout);
			return h;

		} else {
			return onCreateHolder(parent, viewType);
		}
	}


	public abstract RecyclerView.ViewHolder onCreateHolder(ViewGroup parent, int viewType);


	@Override
	public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
		int itemType = getItemViewType(position);
		if (itemType == ITEM_VIEW_TYPE_LOADING) {
			LoadingViewHolder loadingHolder = (LoadingViewHolder) holder;
			// loadingHolder.loadingLayout.setState(LoadingState.IDLE);
		} else if (itemType == ITEM_VIEW_TYPE_HEADER) {

		} else {
			if (hasHeaderView()) {
				position--;
			}
			final int dataPosi = position;
			final long itemId = getItemId(dataPosi);

			onBindHolder(holder, position);
			holder.itemView.setTag("" + position);
			if (holder != null) {
				if (mClickListener != null) {
					holder.itemView.setOnClickListener(new View.OnClickListener() {

						@Override
						public void onClick(View v) {
							mClickListener.onItemClick(null, holder.itemView, dataPosi, itemId);

						}
					});
				}

				if (mLongClickListener != null) {
					holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {

						@Override
						public boolean onLongClick(View v) {
							return mLongClickListener
									.onItemLongClick(null, holder.itemView, dataPosi, itemId);
						}

					});
				}

			}
		}
	}


	public abstract void onBindHolder(RecyclerView.ViewHolder holder, int position);


	/**
	 * 获取 Holder 所代表的前一个View的所在的position
	 * 
	 * @param holder
	 * @return
	 */
	public int getOldPosition(RecyclerView.ViewHolder holder) {
		Object tag = holder.itemView.getTag();
		int oldPosi = -1;
		if (tag != null) {
			String tagStr = String.valueOf(tag);
			if (!TextUtils.isEmpty(tagStr)) {
				try {
					oldPosi = Integer.valueOf(tagStr);
				} catch (NumberFormatException e) {
					oldPosi = -1;
				}
			}
		}
		return oldPosi;
	}

	protected int ITEM_VIEW_TYPE_ITEM = 0;
	protected int ITEM_VIEW_TYPE_LOADING = 1;
	protected int ITEM_VIEW_TYPE_HEADER = 2;


	@Override
	public int getItemViewType(int position) {
		if (position == 0 && hasHeaderView()) {
			return ITEM_VIEW_TYPE_HEADER;
		}

		if (hasLoadingLayout() && position + 1 == getItemCount()) {
			return ITEM_VIEW_TYPE_LOADING;
		}
		return ITEM_VIEW_TYPE_ITEM;
	}


	@Override
	public int getItemCount() {
		int notDataItemCount = 0;
		if (hasLoadingLayout()) {
			notDataItemCount++;
		}
		if (hasHeaderView()) {
			notDataItemCount++;
		}
		return dataList.size() + notDataItemCount;

	}


	/**
	 * 获取有效数据的大小
	 * 
	 * @return
	 */
	public int getDataCount() {
		return dataList.size();
	}


	public List<T> getData() {
		return dataList;
	}


	/**
	 * 设置点击监听
	 * 注意：改方法不设置访问权限， 意味着只有同一个package的class可以访问这个方法，这个是故意这么设置的，因为我希望这个方法只能在RecyclerListViewBaseFragment中调用。
	 * 
	 * @param listener
	 */
	public void setOnItemClickListener(AdapterView.OnItemClickListener listener) {
		mClickListener = listener;
	}


	public void setOnItemLongClickListener(AdapterView.OnItemLongClickListener listener) {
		mLongClickListener = listener;
	}

	static class LoadingViewHolder extends RecyclerView.ViewHolder {

		public LoadingLayout loadingLayout;


		public LoadingViewHolder(LoadingLayout v) {
			super(v);
			loadingLayout = v;
		}

	}

	/**
	 * Header View
	 * 
	 * @author Vivi Peng
	 *
	 */
	static class HeaderViewHolder extends RecyclerView.ViewHolder {

		public View headerView;


		public HeaderViewHolder(View hv) {
			super(hv);
			headerView = hv;
		}

	}

	private LoadingLayout mLoadingLayout;
	private View mHeaderView;


	public void setLoaidngLayout(LoadingLayout loadingLayout) {
		mLoadingLayout = loadingLayout;
	}


	public void setHeaderView(View headerView) {
		mHeaderView = headerView;
	}


	private boolean hasHeaderView() {
		return mHeaderView == null ? false : true;
	}


	private boolean hasLoadingLayout() {
		return mLoadingLayout == null ? false : true;
	}

}
